<template>
	<el-dialog draggable v-model="dialogShow" :close-on-click-modal="false" width="60%" @close="onClose">
		<el-form ref="formRef" :rules="formRules" :model="formData" size="default" label-width="90px">
			<el-row :gutter="30">
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
					<el-form-item label="部门名称" prop="Name">
						<el-input v-model="formData.Name" placeholder="请输入部门名称" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :xs="12" :sm="6" :md="6" :lg="6" :xl="6" class="mb20">
					<el-form-item label="部门状态" prop="Status">
						<el-switch v-model="formData.Status" inline-prompt active-text="启" inactive-text="禁"></el-switch>
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
					<el-form-item label="部门描述" prop="Desc">
						<el-input v-model="formData.Desc" type="textarea" placeholder="请输入部门描述" maxlength="150"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="onClose" size="default">取 消</el-button>
				<el-button type="primary" @click="onSubmit" size="default">提交</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { Post } from '/@/common/http';
import EMsg from '/@/common/eMsg';

const dialogShow = ref(false);

const formRef = ref();
const formRules = ref({
	Name: [{ required: true, message: '部门名称不可为空', trigger: 'blur' }],
});
const formData = ref({
	ParentId: 0,
	Id: 0,
	Name: '',
	Desc: '',
	DataRang: '',
	Status: true,
});

const onShow = (id: number, parentId: number) => {
	dialogShow.value = true;
	formData.value.Id = id;
	formData.value.ParentId = parentId;
	if (id > 0) {
		Post('SysDept/Get', { Id: id }, (res: any) => {
			formData.value = res.Data;
		});
	}
};
const onClose = () => {
	formRef.value.resetFields();
	dialogShow.value = false;
};
const onSubmit = () => {
	formRef.value.validate((valid: any) => {
		if (valid) {
			if (formData.value.Id > 0) {
				Post('SysDept/Edit', formData.value, (res: any) => {
					EMsg.success(res.Msg);
					onClose();
					emit('refresh');
				});
			} else {
				Post('SysDept/Add', formData.value, (res: any) => {
					EMsg.success(res.Msg);
					onClose();
					emit('refresh');
				});
			}
		}
	});
};

const emit = defineEmits(['refresh']);
defineExpose({
	onShow,
});
</script>
